package com.codeshellme.bookmanager.service.impl;

import com.codeshellme.bookmanager.dao.UmsUserDao;
import com.codeshellme.bookmanager.dto.UmsUser;
import com.codeshellme.bookmanager.service.UmsUserService;
import com.codeshellme.bookmanager.utils.SimpleCode;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service
public class UmsUserServiceImpl implements UmsUserService {

    @Autowired
    private UmsUserDao umsUserDao;

    @Override
    public PageInfo<Map<String, Object>> queryAllUsers(String nickname, Integer role_id, Byte status,
                                                   String date_start, String date_end,
                                                   Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<Map<String, Object>> users = umsUserDao.queryAllUserList(
                nickname, role_id, status, date_start, date_end);
        return new PageInfo<>(users);
    }

    @Override
    public int updateUser(UmsUser user) {
        String nickname = user.getNickname();
        if (nickname != null && !nickname.equals("")) {
            user.setPincode(SimpleCode.getFirstSpell(nickname));
        }

        return umsUserDao.updateUser(user);
    }

    @Override
    public int addUser(UmsUser user) {
        String nickname = user.getNickname();
        if (nickname != null && !nickname.equals("")) {
            user.setPincode(SimpleCode.getFirstSpell(nickname));
        }

        return umsUserDao.addUser(user);
    }

    @Override
    public int deleteUserById(Integer userId) {
        return umsUserDao.deleteById(userId);
    }

    @Override
    public Boolean verifyUserName(String username) {
        UmsUser user = umsUserDao.findByUserName(username);
        if (user == null) {
            return true;    // 没有找到用户，可用
        } else {
            return false;    // 找到了用户，不可用
        }
    }

}
